package com.perselab.recipe.bean;

import java.util.Date;
import java.util.List;


/**
 * This bean stores a set of filters
 * 
 * @since Aug 30, 2011
 * @author Alecsandru Gabriel DINU
 */
public class Filter {
	private String property;
	private String operation; 
    private Object value;
    
    private boolean orValue = false;
    private boolean predefined = false;
    
    
    public Filter() {
    }
    
    public Filter(String property, Object value, String operation) {
        this.property = property;
        this.operation = operation;
        this.value = value;
    }

    public Filter(String property, Object value, String operation, boolean predefined) {
        this.property = property;
        this.operation = operation;
        this.value = value;
        this.predefined = predefined;
    }
    

    public Filter(String property, Object value, String operation, boolean predefined, boolean orValue) {
        this.property = property;
        this.operation = operation;
        this.value = value;
        this.predefined = predefined;
        this.orValue = orValue;
    }
    
	/**
	 * @return the property
	 */
	public String getProperty() {
		return property;
	}
	
	/**
	 * @param property the property to set
	 */
	public void setProperty(String property) {
		this.property = property;
		if (this.property != null) {
			this.property = this.property.replaceAll("_", ".");
		}
	}
	
	/**
	 * @return the operation
	 */
	public String getOperation() {
		return operation;
	}
	
	/**
	 * @param operation the operation to set
	 */

	public void setOperation(String operation) {
		this.operation = operation;
	}
	
	/**
	 * @return the string value
	 */
	public Object getValue() {
		return value;
	}
	
	
	/**
	 * @param value the value to set
	 */
	public void setValue(Object value) {
		this.value = value;
	}
	
	/**
	 * @param value the value to set
	 */
	public void setStringValue(String value) {
		this.value = value;
	}	
    
	/**
	 * Sets for Long values
	 * @param value
	 */
	public void setLongValue(Long value) {
		this.value = value;
	}
	
	/**
	 * Sets for Integer values
	 * @param value
	 */
	public void setIntValue(Integer value) {
		this.value = value;
	}	
	
	/**
	 * Sets for Boolean values
	 * @param value
	 */
	public void setBoolValue(Boolean value) {
		this.value = value;
	}		
	
	/**
	 * Sets for Date values
	 * @param value
	 */
	public void setDateValue(Date value) {
		this.value = value;
	}	
	
	/**
	 * Sets for Float values
	 * @param value
	 */
	public void setFloatValue(Float value) {
		this.value = value;
	}	
	
	/**
	 * When the value represents a range, then it
	 * is actually a list of 2 elements
	 * 
	 * @return
	 */
	public Object getRangedValue() {
		return (List<Object>) value; 
	}

	/**
	 * @return the predefined
	 */
	public boolean isPredefined() {
		return predefined;
	}

	/**
	 * @param predefined the predefined to set
	 */
	public void setPredefined(boolean predefined) {
		this.predefined = predefined;
	}
	
	public boolean isOrValue() {
		return orValue;
	}

	public void setOrValue(boolean orValue) {
		this.orValue = orValue;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((operation == null) ? 0 : operation.hashCode());
		result = prime * result
				+ ((property == null) ? 0 : property.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Filter other = (Filter) obj;
		if (operation == null) {
			if (other.operation != null)
				return false;
		} else if (!operation.equals(other.operation))
			return false;
		if (property == null) {
			if (other.property != null)
				return false;
		} else if (!property.equals(other.property))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}
}

